#!/bin/bash
# 2017-04-12
# exec mysql for all mysql pod
#

. /usr/local/bin/logger

sql=$1
if [ ! -n "$1" ]; then
    read -p "请输入sql：" sql
fi
logger $sql

if [ ! -z "$sql" ]; then
	read -p "sql会在所有mysql中执行，确认执行? y/n " confirm
	if [[ $confirm != 'y' ]]; then
		logger "cancel operation."
		exit 0
	fi

fi
name=`cat /proc/sys/kernel/random/uuid| cksum | cut -f1 -d" "`
day=`date +"%F"`
curName="$day-$name"
shellFile="/var/nas/data/tmp/$curName.sh"
sqlFile="/var/nas/data/tmp/$curName.sql"
test ! -d "/var/nas/data/tmp" && mkdir -p "/var/nas/data/tmp" && logger "mkdir -p /var/nas/data/tmp"
echo "$sql" >> $sqlFile
echo "mysql -uroot -pbxn500 < /usr/local/data/tmp/$curName.sql" >> $shellFile
pods=`kubectl get pod | grep mysql | awk '{print $1}'`
logger "begin to exec sql"
for pod in $pods; do
    logger "$pod ..."
    sudo kubectl exec $pod /bin/bash /usr/local/data/tmp/$curName.sh
done

